// pages/orders/detail/index.js
const router = require('../../../utils/router.js')
const api = require('../../../utils/request.js') 
Page({

  /**
   * 页面的初始数据
   */
  data: {
    AskModal: false,
    orderId: null,
    orderDetail: null,
    questionAnswerList: [],
    askContentValue: '',
    workTaskList: []
  },
  showAskModal() {
    this.setData({
      AskModal: true
    })
  },
  hideAskModal () {
    this.setData({
      AskModal: false
    })
  },
  tapNavigateToUrl({ currentTarget }) {
    router.push(currentTarget.dataset.url)
  },
  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function ({ orderId }) {
    this.setData({
      orderId: orderId
    })
    wx.showLoading({
      title: '加载中',
      mask: true
    })
    this.init().finally(() =>{
      wx.hideLoading()
    })
  },
  refreshPage() {
    wx.showLoading({
      title: '正在刷新',
      mask: true
    })
    this.init().finally(() => {
      wx.hideLoading()
    })
  },
  init() {
    return new Promise (resolve => {
      this.getOrderDetail().then(orderDetail => {
        this.setData({
          orderDetail: orderDetail
        })
        this.getQuestionAnswer().then((questionAnswerList) => {
          this.setData({
            questionAnswerList: questionAnswerList
          })
          resolve()
        })
        if (orderDetail.State === 7 || orderDetail.State === 8 || orderDetail.State === 9) {
          this.getConstructionList().then((workTaskList) => {
            this.setData({
              workTaskList: workTaskList
            })
            resolve()
          })
        } else {
          resolve()
        }
      })
    })
  },
  /**
   * 获取施工任务
   */
  getConstructionList() {
    return new Promise(resolve => {
      api._get('/User/Construction/Mission/List', {
        index: 1,
        designInfoID: this.data.orderId
      }, {
          loading: false
        }).then(res => {
          if (res.Code === 100000) {
            resolve(res.Data)
          }
        })
    })
  },
  /**
   * 获取问答信息
   */
  getQuestionAnswer() {
    return new Promise(resolve => {
      api._get('/User/Design/QuestionAnswer/List', {
        designInfoID: this.data.orderId
      },{
        loading: false
      }).then(res => {
        if (res.Code === 100000) {
          resolve(res.Data)
        }
      })
    })
  },
  // 提交问题
  askFormSubmit({ detail}) {
    let askContent = detail.value.askContent
    if (!askContent) {
      wx.showToast({
        icon: 'none',
        title: '问题不可为空！'
      })
      return false
    }
    if (askContent.length > 50) {
      wx.showToast({
        icon: 'none',
        title: '问题不可超过50字！'
      })
      return false
    }
    api._post('/User/Design/QuestionAnswer/Add', {
      designInfoID: this.data.orderId,
      question: askContent
    }).then(res => {
      if (res.Code === 100000) {
        this.setData({
          AskModal: false,
          askContentValue: ''
        })
        this.getQuestionAnswer().then((questionAnswerList) => {
          this.setData({
            questionAnswerList: questionAnswerList
          })
          resolve()
        })
      }
    })
  },
  /**
   * 获取预约单详情
   */
  getOrderDetail() {
    return new Promise(resolve => {
      api._get('/User/Design/Details', {
        designInfoID: this.data.orderId
      },{
        loading: false
      }).then(res => {
        if (res.Code === 100000) {
          resolve(res.Data)
        }
      })
    })
  },
  /**
   * 获取预约单详情
   */
  confirmConstruction() {
    return new Promise(resolve => {
      api._put(`/User/Construction/Confirm?designInfoID=${this.data.orderId}`, {
          loading: false
        }).then(res => {
          if (res.Code === 100000) {
            this.init()
            resolve(res.Data)
          }
        })
    })
  },
  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {
  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {
    wx.showLoading({
      title: '正在刷新',
      mask: true
    })
    this.init().finally(() => {
      wx.stopPullDownRefresh()
      wx.hideLoading()
    })
  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {

  }
})